package com.piece.core.framework.util.math;

import com.piece.core.framework.constant.ExceptionConstants;
import com.piece.core.framework.exception.ServerException;
import com.piece.core.framework.util.basic.I18nUtil;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 散列算法工具
 */
public class DigestUtil {

	private DigestUtil() {
		throw new IllegalStateException("对象禁止实例化");
	}

	/**
	 * 散列算法
	 */
	public static final String ALGO_MD5 = "MD5";

	/**
	 * 散列算法
	 */
	public static final String ALGO_SHA1 = "SHA-1";

	/**
	 * 获取MD5散列算法对象
	 */
	public static final MessageDigest md5() {
		return digest(ALGO_MD5);
	}

	/**
	 * 获取SHA-1散列算法对象
	 */
	public static final MessageDigest sha1() {
		return digest(ALGO_SHA1);
	}

	/**
	 * 获取散列算法对象
	 */
	public static final MessageDigest digest(String algo) {
		try {
			return MessageDigest.getInstance(algo);
		} catch (NoSuchAlgorithmException e) {
			throw new ServerException(I18nUtil.message(ExceptionConstants.RSA_METHOD, algo));
		}
	}
}
